package com.ruoyi.app.common

import cn.hutool.json.JSONUtil
import com.aliyun.green20220302.Client
import com.aliyun.green20220302.models.ImageModerationRequest
import com.aliyun.green20220302.models.VideoModerationRequest
import com.aliyun.green20220302.models.VideoModerationResultRequest
import com.aliyun.teaopenapi.models.Config


/**
 * 使用AK&SK初始化账号Client
 * @param accessKeyId
 * @param accessKeySecret
 * @return Client
 * @throws Exception
 */
@Throws(Exception::class)
fun sourceCheckClient(): Client {

    val config = Config()
        .setAccessKeyId(ACCESS_KEY)
        .setAccessKeySecret(ACCESS_SECRET)
    config.endpoint = "green-cip.cn-shanghai.aliyuncs.com"
    return Client(config)
}

data class CheckResult(

    var pass: Boolean? = true,

    var errorMsg: String? = null
)

fun checkError(errorMsg: String) = CheckResult(false, errorMsg)

fun picContentCheck(imageUrl: String): List<CheckResult> =
    sourceCheckClient().let { client ->
        client.imageModeration(ImageModerationRequest().apply {
            setService("baselineCheck")
            setServiceParameters(JSONUtil.toJsonStr(mapOf("imageUrl" to imageUrl)))
        }).let { imageModerationResponse ->
            imageModerationResponse.takeIf { it.statusCode == 200 }
                ?.run {
                    body.takeIf { it.code == 200 }
                        ?.run {
                            data.result.takeIf { it.isNotEmpty() }
                                ?.run {
                                    first().takeIf { it.label == "nonLabel" }
                                        ?.run {
                                            return listOf(CheckResult())
                                        }
                                        ?: run {
                                            this.filter { it.confidence >= 50.0 }.map {
                                                when (it.label) {
                                                    "pornographic_adultContent" -> checkError("疑似含有成人色情内容")
                                                    "pornographic_adultContent_tii" -> checkError("图中文字疑似含有色情内容")
                                                    "sexual_suggestiveContent" -> checkError("疑似含有疑似低俗或性暗示内容")
                                                    "sexual_partialNudity" -> checkError("疑似含有包含肢体裸露或性感内容")
                                                    "sexual_affectionDisplay" -> checkError("疑似含有亲密行为的内容")
                                                    "political_historicalNihility" -> checkError("疑似含有历史虚无内容")
                                                    "political_historicalNihility_tii" -> checkError("图中文字疑似含有历史虚无信息")
                                                    "political_politicalFigure" -> checkError("疑似含有政治人物的内容")
                                                    "political_politicalFigure_name_tii" -> checkError("图中文字疑似含有领导人姓名")
                                                    "political_politicalFigure_metaphor_tii" -> checkError("图中文字疑似含有对主要领导人的代称、暗喻")
                                                    "political_prohibitedPerson" -> checkError("疑似含有不宜宣传的人物的内容")
                                                    "political_prohibitedPerson_tii" -> checkError("图中文字疑似含有落马官员的姓名")
                                                    "political_taintedCelebrity" -> checkError("疑似含有重大负面的公众人物的内容")
                                                    "political_taintedCelebrity_tii" -> checkError("图中文字疑似含有劣迹艺人的姓名")
                                                    "political_flag" -> checkError("疑似含有国家或地区旗帜")
                                                    "political_map" -> checkError("疑似含有中国地图")
                                                    "political_TVLogo" -> checkError("疑似含有特定电视台台标")
                                                    "political_outfit" -> checkError("疑似含有公务服装")
                                                    "political_religion_tii" -> checkError("图中文字疑似含有宗教元素或者信息")
                                                    "political_racism_tii" -> checkError("图中文字疑似含有歧视的表达内容")
                                                    "violent_armedForces" -> checkError("疑似含有武装元素")
                                                    "violent_crowding" -> checkError("疑似含有人群聚集元素")
                                                    "violent_gunKnives" -> checkError("疑似含有刀具、枪支等内容")
                                                    "violent_gunKnives_tii" -> checkError("图中文字疑似含枪支刀具的描述")
                                                    "violent_horrificContent" -> checkError("疑似含有惊悚、血腥等内容")
                                                    "violent_horrific_tii" -> checkError("图中文字疑似描述暴力、恐怖的内容")
                                                    "contraband_drug" -> checkError("含有疑似毒品等内容")
                                                    "contraband_drug_tii" -> checkError("图中文字疑似描述违禁毒品")
                                                    "contraband_gamble" -> checkError("含有疑似赌博等内容")
                                                    "contraband_gamble_tii" -> checkError("图中文字疑似描述赌博行为")
                                                    "fraud_videoAbuse" -> checkError("图片疑似有隐藏视频风险")
                                                    "fraud_playerAbuse" -> checkError("图片疑似有隐藏播放器风险")
                                                    else -> checkError("图片疑似有隐藏播放器风险")
                                                }
                                            }
                                        }
                                } ?: listOf(CheckResult())
                        } ?: listOf(checkError(body.msg))
                } ?: listOf(checkError("审核过程出错, 请重试"))
        }
    }


data class SubmitResult(

    var result: Boolean? = true,

    var taskId: String? = null,

    var errorMsg: String? = null
)

data class VideoCheckResult(

    /**
     * 1.进行中 2.执行结束
     */
    var status: Int? = 1,

    var pass: Boolean? = false,

    /**
     * 错误原因
     */
    var errorMsg: String? = null,
)

fun errorVideoCheckResult(errorMsg: String) = VideoCheckResult(2, false, errorMsg)

fun submitVideoCheck(videoUrl: String): SubmitResult =
    sourceCheckClient().let { client ->
        client.videoModeration(VideoModerationRequest().apply {
            setService("videoDetection")
            setServiceParameters(JSONUtil.toJsonStr(mapOf("url" to videoUrl)))
        }).let { videoModerationResponse ->
            videoModerationResponse.takeIf { it.statusCode == 200 }
                ?.run {
                    this.body.takeIf { it.code == 200 }
                        ?.run {
                            return SubmitResult(true, data.taskId, null)
                        } ?: run {
                        return SubmitResult(false, null, this.body.message)
                    }
                } ?: SubmitResult(false, null, "审核过程出错, 请重试")
        }
    }


fun videoCheckResult(taskId: String): List<VideoCheckResult> {

    return sourceCheckClient().let { client ->
        client.videoModerationResult(VideoModerationResultRequest().apply {
            setService("videoDetection")
            setServiceParameters(JSONUtil.toJsonStr(mapOf("taskId" to taskId)))
        }).let { videoModerationResultResponse ->
            videoModerationResultResponse.takeIf { it.statusCode == 200 }
                ?.run {

                    when (body.message) {

                        "PROCESSING" -> listOf(VideoCheckResult())
                        "success finished" -> {

                            val result = mutableSetOf<VideoCheckResult>()
                            body.data.frameResult?.frameSummarys?.takeIf { it.isNotEmpty() }
                                ?.run {
                                    result.addAll(map {
                                        when (it.label) {
                                            "pornographic_adultContent" -> errorVideoCheckResult("疑似含有成人色情内容")
                                            "pornographic_adultContent_tii" -> errorVideoCheckResult("图中文字疑似含有色情内容")
                                            "sexual_suggestiveContent" -> errorVideoCheckResult("疑似含有疑似低俗或性暗示内容")
                                            "sexual_partialNudity" -> errorVideoCheckResult("疑似含有包含肢体裸露或性感内容")
                                            "sexual_affectionDisplay" -> errorVideoCheckResult("疑似含有亲密行为的内容")
                                            "political_historicalNihility" -> errorVideoCheckResult("疑似含有历史虚无内容")
                                            "political_historicalNihility_tii" -> errorVideoCheckResult("图中文字疑似含有历史虚无信息")
                                            "political_politicalFigure" -> errorVideoCheckResult("疑似含有政治人物的内容")
                                            "political_politicalFigure_name_tii" -> errorVideoCheckResult("图中文字疑似含有领导人姓名")
                                            "political_politicalFigure_metaphor_tii" -> errorVideoCheckResult("图中文字疑似含有对主要领导人的代称、暗喻")
                                            "political_prohibitedPerson" -> errorVideoCheckResult("疑似含有不宜宣传的人物的内容")
                                            "political_prohibitedPerson_tii" -> errorVideoCheckResult("图中文字疑似含有落马官员的姓名")
                                            "political_taintedCelebrity" -> errorVideoCheckResult("疑似含有重大负面的公众人物的内容")
                                            "political_taintedCelebrity_tii" -> errorVideoCheckResult("图中文字疑似含有劣迹艺人的姓名")
                                            "political_flag" -> errorVideoCheckResult("疑似含有国家或地区旗帜")
                                            "political_map" -> errorVideoCheckResult("疑似含有中国地图")
                                            "political_TVLogo" -> errorVideoCheckResult("疑似含有特定电视台台标")
                                            "political_outfit" -> errorVideoCheckResult("疑似含有公务服装")
                                            "political_religion_tii" -> errorVideoCheckResult("图中文字疑似含有宗教元素或者信息")
                                            "political_racism_tii" -> errorVideoCheckResult("图中文字疑似含有歧视的表达内容")
                                            "violent_armedForces" -> errorVideoCheckResult("疑似含有武装元素")
                                            "violent_crowding" -> errorVideoCheckResult("疑似含有人群聚集元素")
                                            "violent_gunKnives" -> errorVideoCheckResult("疑似含有刀具、枪支等内容")
                                            "violent_gunKnives_tii" -> errorVideoCheckResult("图中文字疑似含枪支刀具的描述")
                                            "violent_horrificContent" -> errorVideoCheckResult("疑似含有惊悚、血腥等内容")
                                            "violent_horrific_tii" -> errorVideoCheckResult("图中文字疑似描述暴力、恐怖的内容")
                                            "contraband_drug" -> errorVideoCheckResult("含有疑似毒品等内容")
                                            "contraband_drug_tii" -> errorVideoCheckResult("图中文字疑似描述违禁毒品")
                                            "contraband_gamble" -> errorVideoCheckResult("含有疑似赌博等内容")
                                            "contraband_gamble_tii" -> errorVideoCheckResult("图中文字疑似描述赌博行为")
                                            "fraud_videoAbuse" -> errorVideoCheckResult("图片疑似有隐藏视频风险")
                                            "fraud_playerAbuse" -> errorVideoCheckResult("图片疑似有隐藏播放器风险")
                                            else -> errorVideoCheckResult("疑似含有色情内容")
                                        }
                                    })
                                }

                            body.data.audioResult?.audioSummarys?.takeIf { it.isNotEmpty() }
                                ?.run {
                                    result.addAll(map {
                                        when (it.label) {
                                            "pornographic_adultContent" -> errorVideoCheckResult("疑似含有成人色情内容")
                                            "pornographic_adultContent_tii" -> errorVideoCheckResult("图中文字疑似含有色情内容")
                                            "sexual_suggestiveContent" -> errorVideoCheckResult("疑似含有疑似低俗或性暗示内容")
                                            "sexual_partialNudity" -> errorVideoCheckResult("疑似含有包含肢体裸露或性感内容")
                                            "sexual_affectionDisplay" -> errorVideoCheckResult("疑似含有亲密行为的内容")
                                            "political_historicalNihility" -> errorVideoCheckResult("疑似含有历史虚无内容")
                                            "political_historicalNihility_tii" -> errorVideoCheckResult("图中文字疑似含有历史虚无信息")
                                            "political_politicalFigure" -> errorVideoCheckResult("疑似含有政治人物的内容")
                                            "political_politicalFigure_name_tii" -> errorVideoCheckResult("图中文字疑似含有领导人姓名")
                                            "political_politicalFigure_metaphor_tii" -> errorVideoCheckResult("图中文字疑似含有对主要领导人的代称、暗喻")
                                            "political_prohibitedPerson" -> errorVideoCheckResult("疑似含有不宜宣传的人物的内容")
                                            "political_prohibitedPerson_tii" -> errorVideoCheckResult("图中文字疑似含有落马官员的姓名")
                                            "political_taintedCelebrity" -> errorVideoCheckResult("疑似含有重大负面的公众人物的内容")
                                            "political_taintedCelebrity_tii" -> errorVideoCheckResult("图中文字疑似含有劣迹艺人的姓名")
                                            "political_flag" -> errorVideoCheckResult("疑似含有国家或地区旗帜")
                                            "political_map" -> errorVideoCheckResult("疑似含有中国地图")
                                            "political_TVLogo" -> errorVideoCheckResult("疑似含有特定电视台台标")
                                            "political_outfit" -> errorVideoCheckResult("疑似含有公务服装")
                                            "political_religion_tii" -> errorVideoCheckResult("图中文字疑似含有宗教元素或者信息")
                                            "political_racism_tii" -> errorVideoCheckResult("图中文字疑似含有歧视的表达内容")
                                            "violent_armedForces" -> errorVideoCheckResult("疑似含有武装元素")
                                            "violent_crowding" -> errorVideoCheckResult("疑似含有人群聚集元素")
                                            "violent_gunKnives" -> errorVideoCheckResult("疑似含有刀具、枪支等内容")
                                            "violent_gunKnives_tii" -> errorVideoCheckResult("图中文字疑似含枪支刀具的描述")
                                            "violent_horrificContent" -> errorVideoCheckResult("疑似含有惊悚、血腥等内容")
                                            "violent_horrific_tii" -> errorVideoCheckResult("图中文字疑似描述暴力、恐怖的内容")
                                            "contraband_drug" -> errorVideoCheckResult("含有疑似毒品等内容")
                                            "contraband_drug_tii" -> errorVideoCheckResult("图中文字疑似描述违禁毒品")
                                            "contraband_gamble" -> errorVideoCheckResult("含有疑似赌博等内容")
                                            "contraband_gamble_tii" -> errorVideoCheckResult("图中文字疑似描述赌博行为")
                                            "fraud_videoAbuse" -> errorVideoCheckResult("图片疑似有隐藏视频风险")
                                            "fraud_playerAbuse" -> errorVideoCheckResult("图片疑似有隐藏播放器风险")
                                            else -> errorVideoCheckResult("疑似含有色情内容")
                                        }
                                    })
                                }

                            return result.takeIf { it.isEmpty() }
                                ?.run {
                                    listOf(VideoCheckResult(2, true))
                                } ?: run { result.toList() }
                        }

                        else -> listOf(errorVideoCheckResult(body.message))
                    }

                } ?: listOf(errorVideoCheckResult("审核过程出错, 请重试"))
        }
    }
}

fun main() {
//    val taskId = submitVideoCheck("https://yanlv-video.oss-cn-shanghai.aliyuncs.com//516931.mp4").taskId!!
//    println(taskId)
    videoCheckResult("vi_f_CVA2JNGanSoX2c9LbuUauB-1zEfAm").forEach { println(it) }
}